package com.tcm.controller.front;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.param.CollectUpdateParam;
import com.tcm.entity.resp.R;
import com.tcm.service.CollectService;
import com.tcm.utils.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 学生题目收藏控制器
 * @author Guqier
 * @version 1.0
 * @date 2023/8/20 16:53
 */
@RestController
@RequestMapping("/stu/collect")
public class StuCollectController {

    @Autowired
    private CollectService collectService;

    /**
     * 学生添加题目收藏
     * @param param 收藏参数
     * @param request 请求对象
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.POST)
    public R addCollect(@RequestBody @Valid CollectUpdateParam param, HttpServletRequest request){
        return collectService.addCollect(param, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 学生取消收藏
     * @param itemId 收藏ID
     * @return 处理结果
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public R canselCollect(@RequestParam Long itemId, HttpServletRequest request){
        return collectService.canselCollect(itemId, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 学生获取收藏列表
     * @param request 请求对象
     * @return 收藏列表
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getStuCollectList(HttpServletRequest request){
        return R.success(collectService.getStuCollectList(AccessTokenUtil.getUserId(request)));
    }

    /**
     * 获取收藏详细
     * @param current 当前收藏序数
     * @param request 请求对象
     * @return 收藏详细
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public R getCollectDetail(@RequestParam Integer current, HttpServletRequest request){
        return R.success(collectService.getCollectDetail(current, AccessTokenUtil.getUserId(request)));
    }

}
